package com.aphrodite.ui.widgets;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * List that contains two row per list item
 * 
 * @author kentelt
 * 
 */
public final class TwoRowList extends List {
	private final Vector list2 = new Vector();
	private int secLineFGcolor;
	private int secLineHLColor = 0xffffff;
	private String tmpString2;

	/**
	 * Create a Two row list instance
	 * 
	 * @param theme
	 * @param width
	 *            of the list
	 * @param height
	 *            of the list
	 */
	public TwoRowList(Theme theme, int width, int height) {
		super(theme, width, height);
	}

	/**
	 * Add an item to the list.
	 * <p>
	 * Equal to calling <code>addItem(item,null,icon)</code>
	 * </p>
	 * 
	 * @param item
	 *            First Line
	 * @param icon
	 *            line icon
	 */
	public final void addItem(String item, Image icon) {
		addItem(item, null, icon);
	}

	/**
	 * Add an item to the list.
	 * 
	 * @param line1
	 *            First line string
	 * @param line2
	 *            Second line string
	 * @param icon
	 *            line icon
	 */
	public final void addItem(String line1, String line2, Image icon) {
		list2.addElement(line2);
		super.addItem(line1, icon);
	}

	/* needs to be overriden. renders the icon and the string item */
	final void renderItem(final Graphics g, final boolean isSelected, final int index, final int y) {
		tmpImage = (Image) icons.elementAt(index);
		tmpString = (String) list.elementAt(index);
		tmpString2 = (String) list2.elementAt(index);
		if (tmpImage != null) {
			// g.drawImage(tmpImage, imagePad, y + (itemHeight / 2) -
			// tmpImage.getHeight() / 2, 0);
			g.drawImage(tmpImage, imagePad, y + itemHeight / 2, Graphics.VCENTER);
		}
		/* draw the first row string */
		if (tmpString != null) {
			g.setColor(isSelected ? theme.highLightFGColor : theme.fgColor);
			if (theme.bmFont != null) {
				theme.bmFont.drawString(g, tmpString, textStart, y + 1, 0);
			} else {
				g.setFont(theme.widgetFont);
				g.drawString(tmpString, textStart, y + 1, 0);
			}
		}
		/* draw the second row string */
		if (tmpString2 != null) {
			g.setColor(isSelected ? secLineHLColor : secLineFGcolor);
			if (theme.bmFont != null) {
				theme.bmFont.drawString(g, tmpString2, textStart, y + 1 + theme.bmFont.getHeight() + 2, 0);
			} else {
				g.setFont(theme.widgetFont);
				g.drawString(tmpString2, textStart, y + 1 + theme.widgetFont.getHeight() + 2, 0);
			}
		}
	}

	final void setItemHeight() {
		if (theme.bmFont != null) {
			itemHeight = theme.bmFont.getHeight() * 2;
		} else {
			itemHeight = theme.widgetFont.getHeight() * 2;
		}
		itemHeight += 4;
	}

	/**
	 * Set the FG color for the second line
	 * 
	 * @param color
	 */
	public final void setSecondLineFGColor(int color) {
		this.secLineFGcolor = color;
		reDrawWidget();
	}

	/**
	 * Set the highlight color for the second line
	 * 
	 * @param color
	 */
	public final void setSecondLineHighlightColor(int color) {
		this.secLineHLColor = color;
	}
}
